/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewBill.java
 *
 * Created on 13-04-2012, 17:01:20
 */
package GUI;

import entities.BillingDetails;
import entities.BillingTestDetail;
import entities.PatientDetails;
import entities.Test;
import java.util.ArrayList;

import java.util.Date;
import javax.swing.JOptionPane;
import model.Bill.TableModelBillTestDetail;
import utilities.AllEntitiesManage;
import utilities.BillManage;
import utilities.DateValidate;
import utilities.SimpleDateFormatter;

/**
 *
 * @author Dell
 */
public class AddNewBill extends javax.swing.JDialog {

    private ArrayList<BillingDetails> billList;
    private ArrayList<Test> testList;
    private ArrayList<PatientDetails> patientList;
    private ArrayList<BillingTestDetail> billingTestList;
    private PatientDetails patientDetails;
    private SimpleDateFormatter sdf = new SimpleDateFormatter();
    private BillManage billManage;
    private MainFunction mf;
    private DateValidate dateValidate = new DateValidate();
    private AddNewBill addNewBill;
    private OptionMessage optionMessage;
    

    private void setModelTest() {
        String[] header = {"Test Name", "Test Cost"};

        TableModelBillTestDetail tmt = new TableModelBillTestDetail(billingTestList);
        tmt.setHeader(header);
        tblBillingTest.setModel(tmt);
    }

    /** Creates new form AddNewBill */
    public AddNewBill(java.awt.Frame parent, boolean modal, BillManage billManage, MainFunction mf, PatientDetails patientDetails, ArrayList<BillingDetails> billList) {
        super(parent, modal);
        initComponents();
        setModelTest();

       



        this.patientDetails = new PatientDetails();
        this.patientDetails = patientDetails;

        this.billList = new ArrayList<BillingDetails>();
        this.billList = billList;


        billingTestList = new ArrayList<BillingTestDetail>();





        txtAddress.setText(patientDetails.getpAddress());
        txtPatientName.setText(patientDetails.getpFName() + " " + patientDetails.getpLName());

        dateValidate = new DateValidate(cbDateBill, cbMonthBill, cbYearBill);

        cbDateBill.setSelectedIndex(sdf.getDay(new Date()));
        cbMonthBill.setSelectedIndex(sdf.getMonth(new Date()));
        cbYearBill.setSelectedIndex(sdf.getYear(new Date()));

        dateValidate = new DateValidate(cbDate, cbMonth, cbYear);

        cbDate.setSelectedIndex(sdf.getDay(patientDetails.getpDOB()));
        cbMonth.setSelectedIndex(sdf.getMonth(patientDetails.getpDOB()));
        cbYear.setSelectedIndex(sdf.getYear(patientDetails.getpDOB()));

        this.billManage = new BillManage(this.billList, testList, mf);
        this.billManage = billManage;
        this.mf = mf;

         optionMessage=new OptionMessage(this);

    }

    public void addNewBillingTest(Test test) {
        BillingTestDetail billingTestDetail = new BillingTestDetail();
        billingTestDetail.setTest(test);
        billingTestDetail.setTestCost(test.getTestCost());
        billingTestList.add(billingTestDetail);
        setModelTest();

        float amount = 0;


        for (int i = 0; i < billingTestList.size(); i++) {
            amount += billingTestList.get(i).getTestCost();

        }

        txtAmount.setText("" + amount);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtAmount = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBillingTest = new javax.swing.JTable();
        btSubmit = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        btAddNewTest = new javax.swing.JButton();
        btDeleteTest = new javax.swing.JButton();
        txtPatientName = new javax.swing.JTextField();
        cbDate = new javax.swing.JComboBox();
        cbMonth = new javax.swing.JComboBox();
        cbYear = new javax.swing.JComboBox();
        cbDateBill = new javax.swing.JComboBox();
        cbMonthBill = new javax.swing.JComboBox();
        cbYearBill = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Date");

        jLabel2.setText("Amount");

        txtAmount.setEditable(false);

        jLabel3.setText("Patient Name");

        txtAddress.setEditable(false);

        jLabel4.setText("Test");

        tblBillingTest.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblBillingTest);

        btSubmit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/add2.png"))); // NOI18N
        btSubmit.setText("Add");
        btSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSubmitActionPerformed(evt);
            }
        });

        btCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/Cancel-16.png"))); // NOI18N
        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        jLabel6.setText("Address");

        jLabel7.setText("Date Of Birth");

        btAddNewTest.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/add2.png"))); // NOI18N
        btAddNewTest.setText("Add New Test");
        btAddNewTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddNewTestActionPerformed(evt);
            }
        });

        btDeleteTest.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/delete2.png"))); // NOI18N
        btDeleteTest.setText("Delete Test");
        btDeleteTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteTestActionPerformed(evt);
            }
        });

        txtPatientName.setEditable(false);

        cbDate.setEnabled(false);

        cbMonth.setEnabled(false);

        cbYear.setEnabled(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Add New Bill");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbDateBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbMonthBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbYearBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtAddress, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtPatientName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btDeleteTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btAddNewTest)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(btSubmit)
                        .addGap(35, 35, 35)
                        .addComponent(btCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbDateBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbMonthBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbYearBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPatientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cbDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btAddNewTest)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btDeleteTest)))
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSubmit)
                    .addComponent(btCancel))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        optionMessage.confirmExit();
    }//GEN-LAST:event_btCancelActionPerformed

    private void btAddNewTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddNewTestActionPerformed
        AllEntitiesManage aem = new AllEntitiesManage();
        testList = new ArrayList<Test>();
        testList = aem.getTestList();

        AddNewBillingTest anbt = new AddNewBillingTest(mf, true, testList, this);
        anbt.setVisible(true);
    }//GEN-LAST:event_btAddNewTestActionPerformed

    private void btDeleteTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteTestActionPerformed


        int i = tblBillingTest.getSelectedRow();

        if(i>=0){
           float amount = 0;
        amount = Float.parseFloat(txtAmount.getText());

        amount -= billingTestList.get(i).getTestCost();

        txtAmount.setText("" + amount);

        billingTestList.remove(i);
        setModelTest();
        }

        
    }//GEN-LAST:event_btDeleteTestActionPerformed

    private void btSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSubmitActionPerformed

        if (billingTestList.size() <= 0) {
            JOptionPane.showMessageDialog(rootPane, "Choose a Test!!");
            return;
        }

        BillingDetails bd = new BillingDetails();
        String dateBill = (cbMonthBill.getSelectedIndex() + 1) + "/" + (cbDateBill.getSelectedIndex() + 1) + "/" + (cbYearBill.getSelectedIndex() + 1900);

        bd.setbDate(sdf.convertStringToDate(dateBill));

        bd.setbAmount(Float.parseFloat(txtAmount.getText()));



        bd.setPatientDetails(patientDetails);


        for (BillingTestDetail billingTestDetail : billingTestList) {
            bd.addBillingTestDetail(billingTestDetail);
        }


        if (billManage.createNewBill(bd)) {
            this.dispose();
        } else {
        }

    }//GEN-LAST:event_btSubmitActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AddNewBill dialog = new AddNewBill(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddNewTest;
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btDeleteTest;
    private javax.swing.JButton btSubmit;
    private javax.swing.JComboBox cbDate;
    private javax.swing.JComboBox cbDateBill;
    private javax.swing.JComboBox cbMonth;
    private javax.swing.JComboBox cbMonthBill;
    private javax.swing.JComboBox cbYear;
    private javax.swing.JComboBox cbYearBill;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblBillingTest;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtAmount;
    private javax.swing.JTextField txtPatientName;
    // End of variables declaration//GEN-END:variables
}
